#! usr/lib64/python2.6
# -*- coding: utf-8 -*-  

__doc__ = """The report.py module is the main funcation of DoublaA."""

__author__ = "DoubleA"
__version__ = '$Revision: 0.01 $'
__date__ = '$Date: 2015-01-13 13:48:03 +0800 (Tue, 13 Jan 2015) $'

from pyh import *
from pyf import PyF,calculate_file_num
from datatransform import DataT


def file_table(file_dir):
    data_list, base_file, change_file = PyF(file_dir).file_analysis()
    
    file_table = table(id='filetable',cl="datalist") 
    file_table << tr(th('No.') 
                    + th('Line #')                     
                    + th('Previous Codes')
                    + th('Type')                             
                    + th('Current Codes')
                    + th('Line #'))
    
    for line in data_list:
        file_table << tr(td(line[0],cl="num")                             
                        + td(DataT(line[1]).data_transform(),cl="bl") 
                        + td(DataT(line[3]).data_transform(),cl="bc") 
                        + td(DataT(line[5]).data_transform(),cl="type")                             
                        + td(DataT(line[4]).data_transform(),cl="cc")
                        + td(DataT(line[2]).data_transform(),cl="cl") ) 
        
    return base_file, change_file, file_table


def file_report(floder_dir, file):
    file_dir = floder_dir + '/' + file
    base_file, change_file, ftable = file_table(file_dir)
    result_name = file.split('.') 
    base_name = (base_file.split('/')[-1]).split('.')[0]
    base_version = (base_file.split('/')[-2])
    change_name = (change_file.split('/')[-1]).split('.')[0]
    change_version = (change_file.split('/')[-2])
       
    file_div = div(id=file_dir,cl="file_list")
    title_div = file_div << div(id="menu"+result_name[0],
                                onclick="showmenu(" +"'" + result_name[0] + "'" + ")",cl='title')
    title_div << span(result_name[0])
    title_div << span(base_version + "<--->" + change_version,cl="version")
            
    info_table = table(id="info")
    info_table << tr(td('Previous File')) << td() << a (base_name,
                                                        href=base_file.strip(),
                                                        target="_blank",cl='blink')
    info_table << tr(td('Current File')) << td() << a (change_name,
                                                       href=change_file.strip(),
                                                       target="_blank",cl='clink')
    
    table_div = file_div << div(id="list"+result_name[0],cl="content",style="display:none")
    table_div << info_table
    table_div << ftable

    return file_div


def page_report(folder_dir=".."):
    total, dif, dif_list = calculate_file_num(folder_dir)

    page = PyH('Table Page')

    page << style(PyF("cssfile.css").read_file())
    page << script(PyF("jsfile.js").read_file())
    
    page << div(id="header") << h1("Result Report")
    
    total_div = page << div() 
    total_div << h2("Total")    
    total_table = total_div << table(cl="total") << tr(th('Total') 
                                         + th('Different HTML')
                                         + th('Diff Rate'),
                                         cl='total')
    total_table << tr(td(total)
                      + td(dif)
                      + td("%.2f%%" % (float(dif)/float(total)*100)),
                      cl='total')
    
    page << h2("List")

    for file_name in dif_list:
	if ".txt" in file_name:
            page  << file_report(folder_dir, file_name)
    
    page.printOut('123.html')
    
if __name__ == "__main__":
    print __doc__
    print __version__
    print page_report()
#     print file_table("../TestFile/testfile.txt")
